<script type="text/javascript">
  var aho;
  var aho2;
  var TrainStations, BusStops,SubWays,NearbyPlaces;
  var glob;
  var output = '';   
  
  function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
    var R = 6371; // Radius of the earth in km
    var dLat = deg2rad(lat2-lat1);  // deg2rad below
    var dLon = deg2rad(lon2-lon1); 
    var a = 
      Math.sin(dLat/2) * Math.sin(dLat/2) +
      Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
      Math.sin(dLon/2) * Math.sin(dLon/2); 
    
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c; // Distance in km
    return Math.round(d*100)/100;
  }

  function deg2rad(deg) {
    return deg * (Math.PI/180)
  }
  
  var nearby;
  var request;
   
  var counter=1;
    
  var result;
  var marker;
  var service;
  var map;
  
  function initialize() {          
    var mapOptions = {
            center: new google.maps.LatLng(-34.397, 150.644),
            zoom: 8,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
    
    map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);             
    var geocoder = new google.maps.Geocoder();

    var start ;
    map.setZoom(11);
    var infowin;
    
<?php
  foreach ($Listings as $listing) {
    $link = '<a href="' . SITE_WEBROOT . $listing["Listing"]["url_title"] . '-l' . $listing["Listing"]["id"] . '.html">' . htmlspecialchars($listing["Listing"]["title"]) . '</a>';
    $link = str_replace('"', "'", $link);
    $out = '<div id="listing-rate" style="vertical-align: middle;padding-bottom: 2px;padding-top: 2px;width: 165px">';
    
    if ((($i_AVGRating * 2) % 2) == 0) {
      $i_MaxEn = $i_AVGRating;
      $i_MaxDis = 5 - $i_MaxEn;
      $b_Half = false;
    } 
    else {
      $i_MaxEn = $i_AVGRating - 0.5;
      $i_MaxDis = 5 - ($i_MaxEn + 1);
      $b_Half = true;
    }
    
    for ($i = 0; $i < $i_MaxEn; $i++) :
      $out = $out . '<img src="' . SITE_WEBROOT . 'img/stars_enabled.png" width="15"/>';
    endfor;
    
    if ($b_Half):
      $out = $out . '<img src="' . SITE_WEBROOT . 'img/stars_half.png" width="15"/>';
    endif;
    
    for ($i = 0; $i < $i_MaxDis; $i++) :
      $out = $out . '<img src="' . SITE_WEBROOT . 'img/stars_disabled.png" width="15"/>';
    endfor;
    
    $out = $out . '<span style="float: right;">&nbsp;&nbsp;' . $i_TotalReview . ' мнения</span>';
    $out = $out . '<br/>' . $listing['Listing']['address_1'];
    $out = $out . '<div style="clear: both;"></div>';
    $out = $out . '</div>';

    $out = str_replace('"', "'", $out);
?>
                                                             
    start = '<?= $listing['Listing']['address_1']; ?>';
                        
    geocoder.geocode( { 'address': start}, function(results2, status) {
      setTimeout('', 100);
      if (status == google.maps.GeocoderStatus.OK) {                                         
        marker = new google.maps.Marker({ map: map, position:results2[0].geometry.location });
        
        var contentstr="<div class='wam-googft-info-window'><?= $link ?><br><?= $out ?></div>";
        marker.content=contentstr;
        marker.setVisible(false);
        
        var veh_infowindow = new google.maps.InfoWindow({ content:contentstr });

        google.maps.event.addListener(marker, 'click', function() {
                            veh_infowindow.setContent(this.content);
                            veh_infowindow.open(map,this);
        });
        map.setCenter(results2[0].geometry.location);
        // marker.setPosition(results2[0].geometry.location);
        marker.setVisible(true);                      
      }
    });
<?php 
  } 
  
  foreach ($ListingsTags as $item) {
    foreach($item['Listing'] as $listing){
    $link = '<a href="' . SITE_WEBROOT . $listing["url_title"] . '-l' . $listing["id"] . '.html">' . htmlspecialchars($listing["title"]) . '</a>';
    $link = str_replace('"', "'", $link);
    $out = '<div id="listing-rate" style="vertical-align: middle;padding-bottom: 2px;padding-top: 2px;width: 165px">';
    
    if ((($i_AVGRating * 2) % 2) == 0) {
      $i_MaxEn = $i_AVGRating;
      $i_MaxDis = 5 - $i_MaxEn;
      $b_Half = false;
    } 
    else {
      $i_MaxEn = $i_AVGRating - 0.5;
      $i_MaxDis = 5 - ($i_MaxEn + 1);
      $b_Half = true;
    }
    
    for ($i = 0; $i < $i_MaxEn; $i++) :
      $out = $out . '<img src="' . SITE_WEBROOT . 'img/stars_enabled.png" width="15"/>';
    endfor;
    
    if ($b_Half):
      $out = $out . '<img src="' . SITE_WEBROOT . 'img/stars_half.png" width="15"/>';
    endif;
    
    for ($i = 0; $i < $i_MaxDis; $i++) :
      $out = $out . '<img src="' . SITE_WEBROOT . 'img/stars_disabled.png" width="15"/>';
    endfor;
    
    $out = $out . '<span style="float: right;">&nbsp;&nbsp;' . $i_TotalReview . ' мнения</span>';
    $out = $out . '<br/>' . $listing['Listing']['address_1'];
    $out = $out . '<div style="clear: both;"></div>';
    $out = $out . '</div>';

    $out = str_replace('"', "'", $out);
    ?>
                            
                                 
    start = '<?= $listing['Listing'][0]['address_1']; ?>';
                        
    geocoder.geocode( { 'address': start}, function(results2, status) {
      setTimeout('', 100);
      if (status == google.maps.GeocoderStatus.OK) {                                         
        marker = new google.maps.Marker({ map: map, position:results2[0].geometry.location });
        
        var contentstr="<div class='wam-googft-info-window'><?= $link ?><br><?= $out ?></div>";
        marker.content=contentstr;
        marker.setVisible(false);
        
        var veh_infowindow = new google.maps.InfoWindow({ content:contentstr });

        google.maps.event.addListener(marker, 'click', function() {
            veh_infowindow.setContent(this.content);
            veh_infowindow.open(map,this);
        });
        
        map.setCenter(results2[0].geometry.location);
        // marker.setPosition(results2[0].geometry.location);
        marker.setVisible(true);                      
      }
    });
<?php 
    }
   } 
?>
  }
  
  google.maps.event.addDomListener(window, 'load', initialize);
</script>

<style type="text/css">
  #map-canvas { height: 200px; }
</style>

<div class="listings">
  <?php echo '<br>'; ?>

  <!-- tuannguyen2042: filter -->
  <script type="text/javascript">
    function loadChildCats(catId, locId){
      jQuery(".progress-indicator").fadeIn();
      
      jQuery.ajax({
        type: "GET",
        dataType: "html",
        evalScripts: true,
        url: "<?php echo Router::url(array('controller' => 'inits', 'action' => 'ajaxGetChildCats')) ?>/" + catId + "/" + locId,
        success: function(data, textStatus){
          jQuery(".progress-indicator").fadeOut();
          
          if (data.length > 5)            
            $("#childcats"+catId).html(data).show();
                      
        }
      });
    }
    
    function filterListings(){           
      jQuery(".progress-indicator").fadeIn();
      
      jQuery.ajax({
        type: "GET",
        dataType: "html",
        evalScripts: true,
        url: "<?php echo Router::url(array('controller' => 'listings', 'action' => 'ajaxFilterSearch')) ?>",
        data: jQuery("#filterform").serialize(),
        success: function(data, textStatus){
          jQuery(".progress-indicator").fadeOut();
          jQuery("#searchResult").html(data);
        }        
      });
    }
  </script>
  <!-- end -->     


<?php if (count($locations) > 0 || count($categories) > 0): ?>
<div id="filter" class="box">
  <div class="box-caption-fixed">Filter</div>
    <div class="box-content-fixed ">
      <form method="POST" action="" id="filterform">
        <input type="hidden" name="searchstr" value="<?php echo $searchstring; ?>" />        
        <input type="hidden" name="searchnearby" value="<?php echo $searchnearby; ?>" />
      
        <table border="0" cellpadding="5">
          <?php if (count($locations) > 0): ?>
            <tr valign="top">
              <td>
                <p><strong>Locations</strong></p>
                <div>
                  <?php foreach($locations as $location): ?>
                  <ul class="filtercat">
                    <li>
                      <input type="checkbox" onclick="filterListings()" name="loc<?php echo $location['Location']['id']; ?>" value="<?php echo $location['Location']['id']; ?>" style="margin-top:3px;" />                                           
                      <a href="<?php echo SITE_WEBROOT . $location['Location']['url_title']; ?>"><?php echo htmlspecialchars($location['Location']['name']); ?></a>                                                                                     
                    </li>
                  </ul>                  
                  <?php endforeach; ?>
                </div>
              </td>
            </tr>
          <?php endif; ?>
          
          <?php if (count($categories) > 0): ?>
          <tr valign="top">
            <td>
              <p><strong>Categories</strong></p>
              <div>                  
                <?php 
                  foreach($categories as $category): 
                    $category_name = trim($category['Category']['name']);                                            
                ?>
                <ul class="filtercat">
                  <li>
                    <input type="checkbox" onclick="filterListings()" name="cat<?php echo $category['Category']['id']; ?>" value="<?php echo $category['Category']['id']; ?>" style="margin-top:3px;" />
                    <a href="<?php echo SITE_WEBROOT.$category['Category']['url_title']; ?>"><?php echo $category_name; ?></a>                      
                  </li>
                </ul>                  
                <?php endforeach; ?>
              </div>
            </td>
          </tr>
        <?php endif; ?>
        </table>
      </form>
    </div>
  </div>
</div>
<?php endif; ?>

<br />

<div id="searchResult">
  <table cellspacing="0" cellpadding="0" width="100%">
    <?php
    $i = 0;
    foreach($ListingsTags as $item){
      $class = null;
      if ($i++ % 2 == 0) {
        $class = ' class="altrow"';
      }
      
      foreach($item['Listing'] as $listing ){
        if(isset($listing)){
    ?>
  
    <tr<?php echo $class; ?>>
      <td valign="top" style="padding-right: 5px; width: 125px;">
        <div class="preview" style="margin-bottom: 10px;">
          <img src="http://open.thumbshots.org/image.pxf?url=<?php echo urlencode($listing["url"]); ?>" alt="<?php echo htmlspecialchars($listing["url"]); ?>" />
        </div>
      </td>
      <td valign="top" style="padding-right: 5px;">
        <div class="title">
          <?php echo '<a href="' . SITE_WEBROOT . $listing["url_title"] . '-l' . $listing["id"] . '.html">' . htmlspecialchars($listing["title"]) . '</a>'; ?>
        </div>
        <div class="url"><?php echo htmlspecialchars($listing["url"]); ?></div>
        <p>
        <?php
          echo $htmlToText->html2text($listing["description"], 160, true);
          echo '</p>';
                       
          $ts = explode('<br/>',$listing['Listing'][0]['train_stations']);
          $bs = explode('<br/>',$listing['Listing'][0]['bus_stations']);
          $ss = explode('<br/>',$listing['Listing'][0]['subway_stations']);
        ?>
        
        <!--  <strong>Distance:</strong><div><?=round($listing[0]['distance'])?> m<div>-->
        <?php if (($ts[0]!=='')){ ?>
        <strong>Train Station</strong>
        <div><?php echo $ts[0]; } ?></div>
        
        <?php if (($bs[0]!=='')){ ?>
        <strong>Bus Station</strong>
        <div><?php echo $bs[0];}?></div>
        
        <?php if (($ss[0]!=='')){ ?>
        <strong>Subway Station</strong>
        <div><?php echo $ss[0]; } ?></div>            
      </td>
    </tr>
    <?php  
       }
      }
    }
  
    foreach ($Listings as $listing) {
      $class = null;
      if ($i++ % 2 == 0) {
        $class = ' class="altrow"';
      }
    ?>
    
    <tr<?php echo $class; ?>>
      <td valign="top" style="padding-right: 5px; width: 125px;">
        <div class="preview" style="margin-bottom: 10px;">
          <img src="http://open.thumbshots.org/image.pxf?url=<?php echo urlencode($listing["Listing"]["url"]); ?>" alt="<?php echo htmlspecialchars($listing["Listing"]["url"]); ?>" />
        </div>
      </td>
      <td valign="top" style="padding-right: 5px;">
        <div class="title">
          <?php echo '<a href="' . SITE_WEBROOT . $listing["Listing"]["url_title"] . '-l' . $listing["Listing"]["id"] . '.html">' . htmlspecialchars($listing["Listing"]["title"]) . '</a>'; ?>
        </div>
        <div class="url"><?php echo htmlspecialchars($listing["Listing"]["url"]); ?></div>
        <p>
          <?php
          echo $htmlToText->html2text($listing["Listing"]["description"], 160, true);
          echo '</p>';
                       
          $ts=explode('<br/>',$listing['Listing']['train_stations']);
          $bs=explode('<br/>',$listing['Listing']['bus_stations']);
          $ss=explode('<br/>',$listing['Listing']['subway_stations']);
          ?>
          
          <!-- <strong>Distance:</strong><div><?=round($listing['distance'])?> m<div>
          <strong>Nearby Lat/Long:</strong><div><?=($listing['nearbylatlong'])?> m<div>-->
          <?if (($ts[0]!=='')){?>
          <strong>Train Station</strong>
          <div><? echo $ts[0];}?></div>
          <?if (($bs[0]!=='')){?>
          <strong>Bus Station</strong>
          <div><? echo $bs[0];}?></div>
          <?if (($ss[0]!=='')){?>
          <strong>Subway Station</strong>
          <div><? echo $ss[0];}?></div>
      </td>
    </tr>
    <?php } ?>
  </table>
</div>

